package android.slc.appbase.api.slc.callback;

import android.slc.appbase.R;
import android.slc.appbase.api.slc.callback.po.SlcEntity;
import android.slc.appbase.api.slc.config.ApiConfig;
import android.slc.appbase.api.slc.converter.SlcEntityErrorException;
import android.slc.appbase.api.slc.utils.SlcApiUtils;
import android.slc.appbase.ui.utils.LoadingUtils;
import android.slc.or.SlcCallbackConfig;

import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;

import java.net.ConnectException;

import io.reactivex.rxjava3.annotations.NonNull;
import retrofit2.HttpException;

public class SlcObserverProxyImpl<T> implements SlcObserverProxy<T> {
    protected SlcCallbackConfig mSlcCallbackConfig;
    protected SlcObserverProxyCallback<T> slcObserverProxyCallback;

    public SlcObserverProxyImpl(SlcObserverProxyCallback<T> slcObserverProxyCallback) {
        this(SlcCallbackConfig.defSlcCallbackConfig, slcObserverProxyCallback);
    }

    public SlcObserverProxyImpl(SlcCallbackConfig slcCallbackConfig, SlcObserverProxyCallback<T> slcObserverProxyCallback) {
        this.mSlcCallbackConfig = slcCallbackConfig;
        this.slcObserverProxyCallback = slcObserverProxyCallback;
    }

    @Override
    public void onStart() {
        if (this.mSlcCallbackConfig.isIsShowDialog()) {
            LoadingUtils.showLoadingDialog(String.valueOf(hashCode()), this.mSlcCallbackConfig.getDialogMsg());
        }
    }

    @Override
    public void onNext(@NonNull T data) {
        onFinish();
        slcObserverProxyCallback.onSucceed(data);
    }

    @Override
    public void onError(@NonNull Throwable e) {
        e.printStackTrace();
        onFinish();
        int errorCode = 0;
        String errorMessage = e.getMessage();
        if (e instanceof SlcEntityErrorException) {
            errorCode = ((SlcEntityErrorException) e).getErrorCode();
        } else if (e instanceof HttpException) {
            SlcEntity<T> errorEntity = SlcApiUtils.getMsgByErrorBody((HttpException) e);
            errorCode = errorEntity.getCode();
            errorMessage = errorEntity.getMsg();
        } else if (e instanceof ConnectException) {
            errorCode = ApiConfig.Value.NORMAL_ERROR;
            errorMessage = StringUtils.getString(R.string.net_date_connection_failure);
        }
        showToast(errorCode, errorMessage);
        slcObserverProxyCallback.onFailed(errorCode, errorMessage);
    }

    @Override
    public void onComplete() {

    }

    protected void onFinish() {
        if (mSlcCallbackConfig.isIsShowDialog() && mSlcCallbackConfig.isIsAutoDismissDialog()) {
            dismissDialog();
        }
    }

    protected void dismissDialog() {
        LoadingUtils.dismissLoadingDialog(String.valueOf(hashCode()));
    }

    /**
     * 显示toast
     *
     * @param code
     * @param msg
     */
    protected void showToast(int code, String msg) {
        if (mSlcCallbackConfig.isIsShowToast()) {
            switch (code) {
                case ApiConfig.Value.BUSINESS_EXCEPTIONS:
                case ApiConfig.Value.NORMAL_UNAUTHORIZED:
                case ApiConfig.Value.NORMAL_ERROR:
                case ApiConfig.Value.A_KNOWN_ERROR:
                case ApiConfig.Value.RESULT_UPLOAD_FAILURE:
                    ToastUtils.showShort(msg);
                    break;
                default:
                    ToastUtils.showShort(mSlcCallbackConfig.getToastRes());
                    break;
            }
        }
    }

}
